package com.atguigu.yygh.gateway.config;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * @author mgx
 * @version 1.0
 * @description: 统一跨域处理
 * @date 2022/8/29 16:13
 */
@SpringBootConfiguration
public class CorsConfig {
    @Bean
    public CorsWebFilter getCorsWebFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 创建一个配置对象，对请求的方法，远程ip，请求头【token】进行限制
        config.addAllowedMethod("*");
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}
